package com.hpxx.ylzswl.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hpxx.ylzswl.R;
import com.hpxx.ylzswl.base.BaseActivity;
import com.hpxx.ylzswl.dialog.BaseDialog;
import com.hpxx.ylzswl.dialog.QuitLoginDialog;
import com.hpxx.ylzswl.dialog.UpDataVersionDialog;
import com.hpxx.ylzswl.https.HttpAddress;
import com.hpxx.ylzswl.https.RequestParams;
import com.hpxx.ylzswl.utils.ConstantsUtils;
import com.universal.frame.appupdate.UpdateAppDialog;
import com.universal.frame.generalutils.ApkUtil;
import com.universal.frame.generalutils.CacheCleanManager;
import com.universal.frame.generalutils.GeneralUtil;
import com.universal.frame.generalutils.JsonUtil;
import com.universal.frame.generalutils.NetWorkUtil;
import com.universal.frame.generalutils.SharedPreferencesUtil;
import com.universal.frame.generalutils.ToastUtil;
import com.universal.frame.okhttp.OKHttpUtils;
import com.universal.frame.okhttp.ResultCallBack;

import java.io.IOException;

import okhttp3.Request;

import static com.activeandroid.Cache.getContext;

/**
 * Created by Administrator on 2017/11/9.
 */

public class SetActivity extends BaseActivity implements View.OnClickListener,ResultCallBack{

    private RelativeLayout rl_call_tel,rl_clear_sd
            ,rl_question,rl_aboot,rl_updata;
    private TextView tv_clear_text;
    private UpDataVersionDialog versionDialog;
    private String severTel;
    private TextView tv_severTel;
    private QuitLoginDialog mQuitLoginDialog;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);
        back();
        setTitle("设置");
    }
    //版本更新
    private void updatePost(String appType, String version){
        OKHttpUtils.postAsync(this
                , HttpAddress.UPDATE_VISON
                , new RequestParams(this).getUpdateVersionParams(appType,version), this
                ,false,1);
    }
    @Override
    public void initView() {
        severTel = SharedPreferencesUtil.getString(getApplicationContext(), ConstantsUtils.USER_SERVERTEL);
        rl_call_tel = (RelativeLayout) findViewById(R.id.rl_call_tel);
        rl_clear_sd = (RelativeLayout) findViewById(R.id.rl_clear_sd);
        rl_question = (RelativeLayout) findViewById(R.id.rl_question);
        rl_aboot = (RelativeLayout) findViewById(R.id.rl_aboot);
        rl_updata = (RelativeLayout) findViewById(R.id.rl_updata);
        tv_clear_text = (TextView) findViewById(R.id.tv_clear_text);
        tv_severTel = (TextView) findViewById(R.id.tv_severTel);
        tv_severTel.setText(severTel);
        getCache(tv_clear_text);
        rl_call_tel.setOnClickListener(this);
        rl_clear_sd.setOnClickListener(this);
        rl_question.setOnClickListener(this);
        rl_aboot.setOnClickListener(this);
        rl_updata.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_call_tel: //客服电话
                Intent intentCallPhone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + severTel));
                startActivity(intentCallPhone);
                break;

            case R.id.rl_clear_sd: //清除缓存
                quitLoginDialog();
                break;

            case R.id.rl_question: // 常见问题
                if (NetWorkUtil.isNetWorkConnected(this)) {
                    startActivity(new Intent(this,CommonQuestionActivity.class));
                }else {
                    ToastUtil.showToast(this, R.string.toast_network_connection_tips);
                }

                break;

            case R.id.rl_aboot: //关于我们
                if (NetWorkUtil.isNetWorkConnected(this)) {
                    startActivity(new Intent(this,AboutUsActivity.class));
                }else {
                    ToastUtil.showToast(this, R.string.toast_network_connection_tips);
                }

                break;

            case R.id.rl_updata: //版本更新
                if (NetWorkUtil.isNetWorkConnected(this)) {
//                    String url = "com.hpxx.ylzswl.https://qd.myapp.com/myapp/qqteam/Androidlite/qqlite_3.6.0.683_android_r109497_GuanWang_537051348_release_10000484.apk";
//                    if (3> (ApkUtil.getAppVersionCode(this))){
//                        new UpdateAppDialog(this, "2",url, 0).show();
//                    }else {
//                        SettingshowDialog();
//                    }
                    updatePost("2", ApkUtil.getAppVersionCode(this)+"");
                }else {
                    ToastUtil.showToast(this, R.string.toast_network_connection_tips);
                }

                break;

            default:
                break;
        }
    }

    private void SettingshowDialog(){
        versionDialog = new UpDataVersionDialog(this, "已经是最新版本，无需更新！",
                R.style.MyDialogStyle, new BaseDialog.LeaveMyDialogListener() {
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.tv_confirm:
                        versionDialog.dismiss();
                        break;
                    default:
                        break;
                }
            }
        });
        versionDialog.show();
    }


    /**
     * 清除缓存
     */
    private void cleanCache() {
        CacheCleanManager.clearAllCache(this);
        ToastUtil.showToast(this,
                R.string.text_clean_ok);
        try {
            tv_clear_text.setText(CacheCleanManager.getTotalCacheSize(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取缓存
     */
    private void getCache(TextView setText) {
        try {
            String cache = CacheCleanManager.getTotalCacheSize(this);
            setText.setText(cache);
        } catch (Exception e) {
            setText.setText("0M");
            e.printStackTrace();
        }
    }

    @Override
    public void onSuccess(String response, int flag) {
        int status = JsonUtil.getInt(response, "status", 0);
        try {
            switch (flag) {
                case 1:
                   UpDataVersionDetail(response);
                    break;

                default:
                    break;
            }
        } catch (Exception e) {
            GeneralUtil.tryShow(getContext(), e);
        }
    }

    private void UpDataVersionDetail(String response) {
        int status = JsonUtil.getInt(response, "status", 0);
        String msg = JsonUtil.getString(response,"msg","");
        if (status == 1){
            String data = JsonUtil.getString(response, "data", "");
            String isnew = JsonUtil.getString(data, "isnew", "");
            String isForced = JsonUtil.getString(data, "isForced", "");
            String url = JsonUtil.getString(data, "url", "");
            if (Integer.parseInt(isnew) == 1){
                new UpdateAppDialog(this, isnew,url, 0).show();
            }else {
                SettingshowDialog();
            }
        }else {
            ToastUtil.showToast(getApplicationContext(),msg);
        }
    }

    @Override
    public void onFailure(Request request, IOException e, int flag) {

    }
    private void quitLoginDialog(){
        mQuitLoginDialog  = new QuitLoginDialog(this,"是否清理缓存", R.style.MyDialogStyle, new BaseDialog.LeaveMyDialogListener() {

            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.bt_quit_qx:
                        mQuitLoginDialog.dismiss();
                        break;
                    case R.id.bt_quit_ok:
                        mQuitLoginDialog.dismiss();
                        cleanCache();
                        break;

                    default:
                        break;
                }
            }
        });
        mQuitLoginDialog.setCanceledOnTouchOutside(false);
        mQuitLoginDialog.show();
    }
}
